package st.spider;

import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;

import org.bson.types.ObjectId;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;

/**
 * Hello world!
 *
 */
public class App_MakeIndex {
	@SuppressWarnings("deprecation")
	public static void main(String[] args) {
		System.out.println("Hello World!");
		try {
			MongoClient mongoClient = new MongoClient("115.159.74.171", 27017);
			DB db = mongoClient.getDB("cult_site");
			db.authenticate("root", new char[] { 'r', 'o', 'o', 't' });
			DBCollection dbc = db.getCollection("movie");
			DBCollection dbci = db.getCollection("movieIndex");

			DBCursor c = dbc.find();
			List<DBObject> l = c.toArray();
			List<DBObject> res = new ArrayList<DBObject>();
			DBObject nIndex;
			for (DBObject o : l) {
				nIndex = new BasicDBObject();
				ObjectId _id = (ObjectId) o.get("_id");
				String movieId = _id.toString();
				String name = (String) o.get("name");
				String updateTime = (String) o.get("updateTime");
				nIndex.put("movieId", movieId);
				nIndex.put("name", name);
				nIndex.put("updateTime", updateTime);
				res.add(nIndex);
			}
			dbci.insert(res);
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
	}
}
